package com.example.apptest.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.app_test.R;
import com.example.apptest.events.EventLEDStatus;
import com.example.apptest.events.EventNotifyChange;
import com.example.apptest.events.EventSpinMove;
import com.example.apptest.utils.BLEHelper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class BLEFragment extends Fragment implements View.OnTouchListener {

    static private BLEFragment instance = null;

    private Button btnRotate, btnSpeed;

    private MySurface surface;
    private BLEHelper bleHelper;
    private String TAG = "BLEFragment";
    private BLEFragment() {}
    private ImageView ledView;

    static public BLEFragment getInstance() {
        if(instance == null) {
            instance = new BLEFragment();
        }
        return instance;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        instance = null;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ble, null);
        btnRotate = view.findViewById(R.id.btn_rotate);
        btnSpeed = view.findViewById(R.id.btn_Speed);
        surface = view.findViewById(R.id.surface);
        btnSpeed.setOnTouchListener(this);
        btnRotate.setOnTouchListener(this);

        view.findViewById(R.id.led_read).setOnTouchListener(this);
        view.findViewById(R.id.led_toggle).setOnTouchListener(this);

        ledView = view.findViewById(R.id.led);
        bleHelper = BLEHelper.getInstance();
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        if(surface != null) {
            surface.stopGraph();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(surface != null) {
            surface.runGraph();
        }
    }

    int ledWrite = 0;
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(view.getId() == R.id.btn_rotate) {
            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                surface.startRotating();
                Log.d(TAG, "rotate pressed");
            } else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                surface.stopRotating();
                Log.d(TAG, "rotate released");
            }
        } else if(view.getId() == R.id.btn_Speed) {
            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                surface.startMoving();
                Log.d(TAG, "speed pressed");
            } else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                surface.stopMoving();
                Log.d(TAG, "speed released");
            }
        } else if(view.getId() == R.id.led_read)  {
            if (motionEvent.getAction() == MotionEvent.ACTION_UP){
                bleHelper.readLEDCharacter();
            }
        } else if(view.getId() == R.id.led_toggle) {
            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                bleHelper.writeLEDCharacter(++ledWrite % 2);
            }
        }
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLEDChanged(EventLEDStatus event) {
        if(event.status == 0x00) {
            ledView.setImageResource(R.color.text_gray);
        } else {
            ledView.setImageResource(R.color.blue);
        }
    }
}